#include<cstdio>
#include<cstring>
const int maxn=100010;
int c[maxn],x,n,t,a[maxn],ls[maxn],lb[maxn];
int low(int x)
{
	return x&(x^(x-1));
}
int sum(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=c[x];
		x-=low(x);
	}
	return ans;
}
void add(int i,int x)
{
	while(i<maxn)
	{
		c[i]+=x;
		i+=low(i);
	}
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		long long ans=0;
		memset(c,0,sizeof(c));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			add(a[i],1);
			ls[i]=sum(a[i]-1);
			lb[i]=sum(maxn-1)-sum(a[i]);			
		}
		memset(c,0,sizeof(c));
		for(int i=n;i>=1;i--)
		{
			add(a[i],1);
			int k1=sum(a[i]-1);
			int k2=sum(maxn-1)-sum(a[i]);
			ans+=k1*lb[i]+k2*ls[i];
		}
		printf("%lld\n",ans);
	}
	return 0;
}

